﻿using FluentValidation;
using static ELS.Components.Pages.TopicManger;

namespace ELS.Models
{
	public class QuestionFluentValidator : AbstractValidator<Question>
	{
		public QuestionFluentValidator()
		{
			RuleFor(x => x.Title).NotEmpty().WithMessage("问题不能为空.");
			RuleFor(x => x.Category).NotEmpty().WithMessage("所属分类不能为空.");
			RuleFor(x => x.Answer1).NotEmpty().WithMessage("答案1不能为空.");
			RuleFor(x => x.Answer2).NotEmpty().WithMessage("答案2不能为空.");
			RuleFor(x => x.Answer3).NotEmpty().WithMessage("答案3不能为空.");
			RuleFor(x => x.CurentAnswerIndex).NotEmpty().WithMessage("正确答案不能为空.");
			RuleFor(x => x.CurentAnswerIndex).LessThan(5).WithMessage("正确答案范围只能是1-4.");
			RuleFor(x => x.CurentAnswerIndex).GreaterThan(0).WithMessage("正确答案范围只能是1-4.");
		}

		public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
		{
			var result = await ValidateAsync(ValidationContext<Question>.CreateWithOptions((Question)model, x => x.IncludeProperties(propertyName)));
			if (result.IsValid)
				return Array.Empty<string>();
			return result.Errors.Select(e => e.ErrorMessage);
		};
	}
}
